package com.turnengine.client.local.faction.bean;

import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.faction.enums.FactionUnited;
import java.io.IOException;

/**
 * The Faction Serializer.
 */
public class FactionSerializer implements IFactionSerializer {

	@Override
	public IFaction readObject(IDataReader reader) throws IOException {

		// Read fields
		int id = reader.readInt();
		String name = reader.readString(true);
		String password = reader.readString(true);
		FactionUnited united = reader.readEnum(FactionUnited.class, true);
		int playerLimit = reader.readInt();

		// Create object
		IFaction object = new Faction();
		object.setId(id);
		object.setName(name);
		object.setPassword(password);
		object.setUnited(united);
		object.setPlayerLimit(playerLimit);
		return object;
	}

	public void writeObject(IDataWriter writer, IFaction object) throws IOException {

		// Get fields
		int id = object.getId();
		String name = object.getName();
		String password = object.getPassword();
		FactionUnited united = object.getUnited();
		int playerLimit = object.getPlayerLimit();

		// Write fields
		writer.writeInt(id);
		writer.writeString(name, true);
		writer.writeString(password, true);
		writer.writeEnum(united, true);
		writer.writeInt(playerLimit);
	}
}
